package com.w.picture.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.w.picture.annotation.RoleCheck;
import com.w.picture.common.BaseResponse;
import com.w.picture.common.ResultUtils;
import com.w.picture.common.UniqueUsernameGenerator;
import com.w.picture.constans.UserConstant;
import com.w.picture.exception.ErrorCode;
import com.w.picture.exception.ThrowUtils;
import com.w.picture.mode.dto.user.*;
import com.w.picture.mode.entity.User;
import com.w.picture.mode.enums.UserRoleEnum;
import com.w.picture.mode.vo.LoginUserVo;
import com.w.picture.mode.vo.UserVo;
import com.w.picture.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author wzp
 * @since 2024/12/7
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest request) {
        long id = userService.userRegister(request);
        return ResultUtils.success(id);
    }

    @PostMapping("/login")
    public BaseResponse<LoginUserVo> userLogin(@RequestBody UserLoginRequest request, HttpServletRequest httpServletRequest) {
        LoginUserVo loginUserVo = userService.userLogin(request, httpServletRequest);
        return ResultUtils.success(loginUserVo);
    }

    @GetMapping("/get/login")
    @RoleCheck(value = UserConstant.DEFAULT_ROLE)
    public BaseResponse<LoginUserVo> getLoginUser(HttpServletRequest httpServletRequest) {
        User user = userService.getLoginUser(httpServletRequest);
        return ResultUtils.success(userService.getLoginUserVo(user));
    }

    @PostMapping("/logout")
    public BaseResponse<Boolean> userLogout(HttpServletRequest httpServletRequest) {
        return ResultUtils.success(userService.userLogout(httpServletRequest));
    }

    @PostMapping("/add")
    @RoleCheck(UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> addUser(@RequestBody UserAddRequest request) {

        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR);
        ThrowUtils.throwIf(StrUtil.hasBlank(request.getUserAccount()), ErrorCode.PARAMS_ERROR, "账号不能为空");
        ThrowUtils.throwIf(request.getUserAccount().length() < 4, ErrorCode.PARAMS_ERROR, "用户名过短");
        ThrowUtils.throwIf(request.getUserAccount().contains(" "), ErrorCode.PARAMS_ERROR, "用户名不能包含空格");

        ThrowUtils.throwIf(userService.checkUserAccount(request.getUserAccount()), ErrorCode.PARAMS_ERROR, "账号已存在");

        User user = new User();
        BeanUtil.copyProperties(request, user);
        user.setUserPassword(userService.encryptPassword("12345678"));
        user.setUserName(UniqueUsernameGenerator.generateUniqueUsername());
        user.setUserRole(UserRoleEnum.USER.getValue());
        user.setShareCode(user.getUserName()); // 使用生成的用户名作为分享码
        user.setUserAvatar("https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png");

        boolean save = userService.save(user);
        ThrowUtils.throwIf(!save, ErrorCode.SYSTEM_ERROR, "添加用户失败");

        return ResultUtils.success(user.getId());
    }

    @GetMapping("/get/vo")
    @RoleCheck(UserConstant.ADMIN_ROLE)
    public BaseResponse<UserVo> getUserVoById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR, "id不能 <= 0");
        User byId = userService.getById(id);
        return ResultUtils.success(userService.getUserVo(byId));
    }

    @GetMapping("/get")
    @RoleCheck(UserConstant.ADMIN_ROLE)
    public BaseResponse<UserVo> getUserById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR, "id不能 <= 0");
        User byId = userService.getById(id);
        return ResultUtils.success(userService.getUserVo(byId));
    }

    @GetMapping("/delete")
    @RoleCheck(UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteUser(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR, "id不能 <= 0");
        return ResultUtils.success(userService.removeById(id));
    }
    @PostMapping("/deleteBatch")
    @RoleCheck(UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteBatchUser(@RequestBody List<Long> ids) {
        ThrowUtils.throwIf(ids == null, ErrorCode.PARAMS_ERROR, "列表不能为空");
        return ResultUtils.success(userService.removeBatchByIds(ids));
    }

    @PostMapping("/update")
    @RoleCheck(UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR, "参数不能为空");
        ThrowUtils.throwIf(request.getId() == null, ErrorCode.PARAMS_ERROR, "id不能为空");
        ThrowUtils.throwIf(StrUtil.isBlank(request.getUserRole()), ErrorCode.PARAMS_ERROR, "角色不能为空");

        User user = new User();
        BeanUtil.copyProperties(request, user);
        return ResultUtils.success(userService.updateById(user));
    }

    @PostMapping("/searchPage")
    @RoleCheck(UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<UserVo>> searchPage(@RequestBody UserQueryRequest request) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR);

        // 修正拼写错误
        Page<User> page = new Page<>(request.getPageNum(), request.getPageSize());

        // 执行分页查询
        Page<User> result = userService.page(page, userService.getQueryWrapper(request));

        // 将查询结果转换为 UserVo 列表
        List<UserVo> userVoList = userService.getUserVoList(result.getRecords());

        // 创建一个新的 Page<UserVo> 并填充数据
        Page<UserVo> voPage = new Page<>(request.getPageNum(), request.getPageSize(), result.getTotal());
        voPage.setRecords(userVoList);

        return ResultUtils.success(voPage);
    }

}
